package com.sys.web.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sys.dao.impl.FileDaoImpl;
import com.sys.domain.File;

@WebServlet("/search")
public class HomeSearch extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if(method.equals("search")) {
			try {
				search(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if (method.equals("searchUper")) {
			try {
				searchUper(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	private void searchUper(HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ServletException, IOException {
		String upId = request.getParameter("upId");
		List<File> selfFiles = new FileDaoImpl().findFileThatUserUpload(upId);
		request.setAttribute("selfFiles", selfFiles);
		request.getRequestDispatcher("/WEB-INF/zuserpages/selfFile.jsp").forward(request, response);
	}

	private void search(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String search = request.getParameter("search");
		List<File> searchFiles = new FileDaoImpl().findFileThatUserSearch(search);
		request.setAttribute("searchFiles", searchFiles);
		request.getRequestDispatcher("/WEB-INF/zuserpages/search.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}